﻿using Microsoft.EntityFrameworkCore;
using RotovapPlatform.Domain.Business.Recipes.Entities;
using RotovapPlatform.Infrastructure.Common.Data;
using System.Diagnostics;
using RotovapPlatform.Domain.Business.Interfaces;

namespace RotovapPlatform.Infrastructure.Business.Recipes;

public class RecipeRepository : IRecipeRepository
{
	private readonly RecipeDbContext _context;

	public RecipeRepository(RecipeDbContext context)
	{
		_context = context;
	}

	public async Task<RecipeDefinition?> GetByIdAsync(Guid recipeId)
	{
		return await _context.Recipes.FindAsync(recipeId);
	}

	public async Task<IEnumerable<RecipeDefinition>> GetAllAsync()
	{
		return await _context.Recipes.ToListAsync();
	}

	public async Task AddAsync(RecipeDefinition recipe)
	{
		await _context.Recipes.AddAsync(recipe);
		await _context.SaveChangesAsync();
	}

	public async Task UpdateAsync(RecipeDefinition recipe)
	{
		_context.Recipes.Update(recipe);
		await _context.SaveChangesAsync();
	}

	public async Task DeleteAsync(Guid recipeId)
	{
		var recipe = await GetByIdAsync(recipeId);
		if (recipe != null)
		{
			_context.Recipes.Remove(recipe);
			await _context.SaveChangesAsync();
		}
	}
}